<?php

session_start();
if(!session_is_registered(myusername)){
$rusername = $_SESSION['username'];
}

include 'config.php';


$filename =$_COOKIE["imagePath"];
list($path, $background, $ext) = split('[/.]', $filename);

        $svg = $_REQUEST["svg_data"];
        $svg_data = urldecode($svg);
        $file = "svgData/" . $background . "_" . $rusername . ".svg";
        $fh = fopen($file, "w") or die("Can't open file");
//		mysql_query("INSERT INTO annotations (file_path, image_id, user_id) VALUES ('svgData/fail.svg',6,3)", $con);
        fwrite($fh, $svg_data);
		
        fclose($fh);


		
$file_path="imageData/".$background.".".$ext;

$result_user_id = mysql_query("SELECT * from users where username='".$rusername."'");
while($row = mysql_fetch_array($result_user_id))
					{		//should ever only return one userid.
							$user_id = $row[id];
							
					}
$result_image_id = mysql_query("SELECT * from images where file_path='".$file_path."'");
while($row = mysql_fetch_array($result_image_id))
					{		
							$image_id = $row[id];
							
					}
			mysql_query("DELETE FROM annotations WHERE file_path = '".$file."'");
			mysql_query("INSERT INTO annotations (file_path, image_id, user_id) VALUES ('".$file."',".$image_id.", ".$user_id.")");

?>
